<?

// If $data['dosearch'], do it; create appropriate SQL query.
if ($data['dosearch'] == 'true') {
	// remove spaces
	foreach($data AS $key=>$val)
		$data[$key] = trim($val);
	
	if ($data['searchin'] == 'all') {
		$sql_query = "SELECT id FROM helpdesk_tickets WHERE email like '%".$data['search']."%' OR subject like '%".$data['search']."%' OR body like '%".$data['search']."%' OR concat(firstname, ' ', lastname) like '%".$data['search']."%' OR domain like '%".$data['search']."%'";
	} else {
		$sql_query = "SELECT id FROM helpdesk_tickets WHERE ".stripslashes($data['searchin'])." like '%".$data['search']."%'";
	}
} else {
	$sql_query = "SELECT id FROM helpdesk_tickets";
}

$dbp = mysql_query($sql_query);
$numrows = mysql_num_rows($dbp);

// set limits:

if (!$numrows AND $data['dosearch'] != 'true') 
	echo $T['no tickets'];
else {
	//if more than 1 reseller, display search 
	if ($numrows > 1 OR $data['dosearch'] == 'true') {
		unset($chk_in);
		unset($chk_show);
		unset($chk_order);
		$chk_in[stripslashes($data['searchin'])] = ' selected';
		$chk_show[$data['show']] = ' selected';
		$chk_order[$data['order']] = ' selected';
		?>

<div align="center">
<form action="<?=$current_url;?>" method="post" name="webcp2" onSubmit="submitonce(this);">
<input type="hidden" name="data[dosearch]" value="true">
<table border="0" cellpadding="0" cellspacing="0" summary="">
	<tr>
		<td class="tblbg">
<table border="0" cellpadding="0" cellspacing="1" summary="">
	<tr>
		<td class="row1">
<table border="0" cellspacing="1" summary="">
	<tr>
		<td class="small"><?=$T['Search'];?></td>
		<td><input type="text" name="data[search]" size="15" maxlength="256" value="<?=$data['search'];?>" class="small"></td>
		<td class="small"><?=$T['in'];?></td>
		<td><select name="data[searchin]" class="small">
	      	<option value="all"<?=$chk_in['all'];?>> <?=$T['All'];?></option>
	      	<option value="email"<?=$chk_in['email'];?>> <?=$T['Email'];?></option>
			<option value="subject"<?=$chk_in['subject'];?>> <?=$T['Subject'];?></option>
			<option value="body"<?=$chk_in['body'];?>> <?=$T['Body'];?></option>
			<option value="domain"<?=$chk_in['domain'];?>> <?=$T['Domain'];?></option>
			<option value="concat(firstname, ' ', lastname)"<?=$chk_in["concat(firstname, ' ', lastname)"];?>> <?=$T['Name'];?></option>
	      	<option value="email"<?=$chk_in['email'];?>> <?=$T['Email'];?></option>
			<option value="subject"<?=$chk_in['subject'];?>> <?=$T['Subject'];?></option>
			<option value="body"<?=$chk_in['body'];?>> <?=$T['Body'];?></option>
			<option value="concat(firstname, ' ', lastname)"<?=$chk_in["concat(firstname, ' ', lastname)"];?>> <?=$T['Name'];?></option>
	      </select>
      	</td>
		<td class="small"><?=$T['display'];?></td>
		<td><select name="data[show]" class="small">
	      	<option value="brief"<?=$chk_show['brief'];?>> <?=$T['Brief'];?></option>
	      	<option value="detailed"<?=$chk_show['detailed'];?>> <?=$T['Detailed'];?></option>
	      	<option value="editable"<?=$chk_show['editable'];?>> <?=$T['Editable'];?></option>
	      </select>
      </td>
      <td><input type="submit" value="<?=$T['Search'];?>" class="small"></td>
	</tr>
</table>
</td>
	</tr>
</table></td>
	</tr>
</table>
<?
if (!$numrows AND $data['dosearch'] == 'true')
	echo $T['no results'];
?>
</div>
</form><br>
	<? } ?>

<div align="center">
<table border="0" cellpadding="0" cellspacing="0"><tr><td>
<? if ($numrows > 25) { ?>
<form action="<?=$current_url;?>" method="post" name="webcp3" onSubmit="submitonce(this);">
<div align="right">
<?=$T['Show'];?>
&nbsp;
<? if ($data['dosearch'] == 'true') { ?>
<input type="hidden" name="data[search]" value="<?=addslashes($data['search']);?>">
<input type="hidden" name="data[searchin]" value="<?=addslashes($data['searchin']);?>">
<input type="hidden" name="data[order]" value="<?=addslashes($data['order']);?>">
<input type="hidden" name="data[show]" value="<?=addslashes($data['show']);?>">
<input type="hidden" name="data[dosearch]" value="true">
<? }
$currentrow = $data['currentrow'];
if ($currentrow) 
	echo '<input type="submit" name="previous" value="'.$T['Previous'].'" class="small">';
$chk_showr[$data['showrows']] = ' selected';
?>
<select name="data[showrows]" class="small">
      	<option value=25<?=$chk_showr[25];?>> 25</option>
      	<option value=50<?=$chk_showr[50];?>> 50</option>
      	<option value=100<?=$chk_showr[100];?>> 100</option>
      	<option value=500<?=$chk_showr[500];?>> 500</option>
</select>
<?
if (($currentrow + $data['showrows']) < $numrows)
	echo '<input type="submit" name="next" value="'.$T['Next'].'" class="small">';
echo '<input type="hidden" name="data[currentrow]" value="'.$currentrow.'">';
echo '<input type="hidden" name="data[oldrows]" value="'.$data['showrows'].'">';
?>
&nbsp;&nbsp;
<? echo $T['Total Records'].($currentrow+1)."-".($currentrow+$data['showrows'])." / ".$numrows; ?>
</div>
</form>
<? } ?>
<? if ($numrows > 0) { ?>
<form action="<?=$current_url;?>" method="post" name="webcp4" onSubmit="submitonce(this);">
<table border="0" cellspacing="1">
	<tr>
		<td class="tbltop"></td>
		<td class="tbltop"><?=$T['Age'];?>
		</td>
		<td class="tbltop"><?=$T['Pri.'];?>
		</td>
		<td class="tbltop"><?=$T['Esc.'];?>
		</td>
		<? if ($data['show'] == 'detailed') { ?>
		<td class="tbltop"><?=$T['Initial'];?>
		</td>
		<? } ?>
		<td class="tbltop"><?=$T['Latest'];?>
		</td>
		<td class="tbltop"><?=$T['Assignment'];?>
		</td>
		<? if ($data['show'] == 'detailed') { ?>
		<td class="tbltop"><?=$T['Domain'];?>
		</td>
		<? } ?>
		<td class="tbltop"><?=$T['From'];?>
		</td>
		<td class="tbltop"><?=$T['Subject'];?>
		</td>
		<? if ($data['show'] == 'detailed') { ?>
		<td class="tbltop"><?=$T['Entries'];?>
		</td>
		<? } ?>
	</tr>
<?
$i = 0;
// Get reseller list

while ($tlist = mysql_fetch_array($dbp)) {
	// select cell bg color
	($i++ % 2) ? ($cell = "row1") : ($cell = "row2");
	$myticket = new techTicket;
	$myticket->openTicket($tlist['id']);
	$current = $myticket->size - 1;
	?>
	<tr >
		<td class="<?=(($myticket->priority[$current] == "Normal") ? "$cell" : $myticket->priority[$current])?>"><input type="checkbox" name="tid[]" value="<?=$tlist['id'];?>"></td>
		<td class="<?=$cell;?>"><nobr><?=$myticket->age[0];?></td>
		<td class="<?=$cell;?>"><div align=center>
		<?
			switch($myticket->priority[$current]) {
				case "Normal":
					echo "--";
					break;
				case "High":
					echo "<img src='/icon/uparw.gif'>";
					break;
				case "Low":
					echo "<img src='/icon/downarw.gif'>";
					break;
				case "Emergency":
					echo "<img src='/icon/emergency.gif'>";
					break;
			}			
		?></div></td>
		<td class="<?=$cell;?>"><div align=center>
		<?
			switch($myticket->escalation[$current]) {
				case "Normal":
					echo "--";
					break;
				case "Level1":
					echo "<img src='/icon/level1.gif'>";
					break;
				case "Level2":
					echo "<img src='/icon/level2.gif'>";
					break;
			}			
		?></div></td>
		<? if ($data['show'] == 'detailed') { ?>
		<td class="<?=$cell;?>"><nobr><?=$myticket->shortsubmissiondate[0];?></td>
		<? } ?>
		<td class="<?=$cell;?>"><nobr><?=$myticket->shortsubmissiondate[$current];?></td>
		<td class="<?=$cell;?>"><nobr><? if ($myticket->assignUsername[$current] != '') { ?><?=$myticket->assignUsername[$current];?> <? if ($data['show'] == 'detailed') { ?>(<?=$myticket->assignName[$current];?>)<? } ?><? } else { ?><div align="center"> -- </div><? } ?></a></td>
		<? if ($data['show'] == 'detailed') { ?>
		<td class="<?=$cell;?>"><nobr><? if ($myticket->domainID[$current] != '') { ?><a href='?cp=domain&url=useradmin&number=<?=$myticket->domainID[$current];?>'><? } if ($myticket->domain[$current] != '') { echo $myticket->domain[$current]; } else { ?><div align="center"> -- </div><? } ?></a></td>
		<? } ?>
		<td class="<?=$cell;?>"><nobr><a href='mailto:<?=$myticket->email[0];?>'><?=$myticket->name[0];?></a></td>
		<td class="<?=$cell;?>"><nobr><a href='<?=$current_url;?>&id=<?=$tlist["id"];?>'><?=((strlen($myticket->subject[0]) < 30) ? $myticket->subject[0] : substr($myticket->subject[0],0,30).'...');?></a></td>
		<? if ($data['show'] == 'detailed') { ?>
		<td class="<?=$cell;?>"><div align=center><?=sizeOf($myticket->id);?></div></td>
		<? } ?>
	</tr>
<? 
}
}
} ?>
</table><br>
<?
if ($numrows > 0) {
	if ($data['dosearch'] == "true") { ?>
		<input type="hidden" name="data[dosearch]" value="true">
		<input type="hidden" name="data[search]" value="<?=$data['search'];?>">
		<input type="hidden" name="data[searchin]" value="<?=$data['searchin'];?>">
		<input type="hidden" name="data[show]" value="<?=$data['show'];?>">
	<? } ?>
	<div align="right">
	<select name="action">
		<option value=""> <?=$T['Select Action'];?></option>
		<? if ($data['show'] == 'editable') { ?><option value="update"> <?=$T['Modify Tickets'];?></option><? } ?>
		<option value="escalate"> <?=$T['Escalate Tickets'];?></option>
		<option value="assign"> <?=$T['Assign Tickets'];?></option>
		<? if ($numrows > 1) { ?>
		<option value="combine"> <?=$T['Combine Tickets'];?></option>
		<? } ?>
		<option value="delete"> <?=$T['Delete Tickets'];?></option>
	</select>
	&nbsp;
	<input type="submit" value="<?=$T['Apply'];?>"></div><? 
} 
?>
</td></tr></table>
</form>
</div>
